package club.sanmubird.travel_culture_wx_demo.biz.source.audio.dao;

import club.sanmubird.travel_culture_wx_demo.biz.source.audio.entity.Audio;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.ResultKey;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: travel_culture_wx_demo
 * @description: audioDao
 * @author: sam
 * @create: 2018-08-05 20:44
 * @update: 2018-08-05 20:44
 * @update: Sam
 **/
@Repository
public class AudioDao {

	public long getAllAudioCount() {
		return Anima.select().from(Audio.class).count();
	}

	public long getAudioCountByCondition(String statement, Object value) {
		return Anima.select().from(Audio.class).where(statement, value).count();
	}

	public List<Audio> getAudiosBySoptId(Long spotId) {
//		return Anima.select().from(Audio.class).where("audio_scenic_spot_sid").eq(spotId).all();
		return Anima.select().from(Audio.class).where(Audio::getAudioScenicSpotSid).eq(spotId).all();
	}

	public Audio getByAudioSid(Long audioSid){
		return Anima.select().from(Audio.class).byId(audioSid);
	}

	public ResultKey save(Audio audio) {
		audio.setAudioCreateTime(System.currentTimeMillis());
		audio.setAudioUpdateTime(System.currentTimeMillis());
		audio.setEnable(true);
		return Anima.save(audio);
	}

	public void batchSave(List<Audio> audioList) {
		audioList.stream().forEach(audio -> {
			audio.setStatus(true);
			audio.setEnable(true);
			audio.setAudioCreateTime(System.currentTimeMillis());
			audio.setAudioUpdateTime(System.currentTimeMillis());
		});
		Anima.saveBatch(audioList);
	}

	public int update(Audio audio) {
		audio.setAudioUpdateTime(System.currentTimeMillis());
		return audio.update();
	}

	public int deleteByAudioSid(Long audioSid) {
		Audio audio = getByAudioSid(audioSid);
		audio.setEnable(false);
		audio.setAudioUpdateTime(System.currentTimeMillis());
		return audio.update();
	}

	/**
	 * 更新上架状态
	 * @param audioSid
	 * @param status
	 * @return
	 */
	public int updateStatus(Long audioSid,Boolean status){
		Audio audio = getByAudioSid(audioSid);
		audio.setStatus(status);
		audio.setAudioUpdateTime(System.currentTimeMillis());
		return audio.update();
	}
}
